package com.coocaa.launcher.wallpaper;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_theme.RP_WallpaperCategory;
import com.coocaa.x.uipackage.widget.CoocaaDraweeView;
import com.coocaa.x.uipackage.widget.SlideFocusView;
import com.tianci.appstore.R;

import java.util.List;

/**
 * Created by luwei on 15-7-3.
 */
public class ScrollTitleBar extends SlideFocusView implements View.OnKeyListener, View.OnFocusChangeListener {
    private LinearLayout container = null;
    private Context context = null;
    private int totalNum = 0;
    private int currentNum = 0;
    private int step = CoocaaApplication.Div(77);
    private List<RP_WallpaperCategory.WallPaper_Category_Item> category_items = null;
    private ImageView pointView = null;
    private FocusViewRevision revision = new FocusViewRevision(CoocaaApplication.Div(40), CoocaaApplication.Div(30));
    private OnTitleSelectedListener titleSelectedListener = null;
    private ScrollView scrollView = null;

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            if (v.equals(pointView)) {
                AnimatorUtils.alpha(pointView, 1, 0);
                container.setVisibility(View.VISIBLE);
                container.setFocusable(true);
                container.setFocusableInTouchMode(true);
                container.requestFocus();
                focusView.getBackground().setAlpha(0);
                pointView.setVisibility(View.GONE);
                Animation animation = AnimationUtils.loadAnimation(context, R.anim.in_leftright);
                animation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        container.getChildAt(currentNum).requestFocus();
                        container.setFocusable(false);
                        container.setFocusableInTouchMode(false);
                        focusView.getBackground().setAlpha(255);
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                container.startAnimation(animation);
                if (titleSelectedListener != null)
                    titleSelectedListener.titleOnSelected();
            }
        } else {
            if (container.isFocused())
                return;
            for (int i = 0; i < container.getChildCount(); i++) {
                if (container.getChildAt(i).isFocused())
                    return;
            }
            container.setVisibility(View.GONE);
            pointView.setVisibility(View.VISIBLE);
            AnimatorUtils.alpha(pointView, 0, 1);
            container.startAnimation(AnimationUtils.loadAnimation(context, R.anim.out_rightleft));
            if (titleSelectedListener != null) {
                enablePointViewFocus(false);
                titleSelectedListener.titleUnSelected();
            }

        }
    }


    public interface OnTitleSelectedListener {
        public void titleSelected(int pos, String title);

        public void titleUnSelected();

        public void titleOnSelected();

        public void onRightKey();
    }

    public ScrollTitleBar(Context context) {
        super(context, R.drawable.hot_seat_focus_bg);
        this.context = context;
        setFocusable(false);
        setFocusableInTouchMode(false);
        addPointView();
        scrollView = new ScrollView(context);
        container = new LinearLayout(context);
        scrollView.setFocusableInTouchMode(false);
        scrollView.setFocusable(false);
        scrollView.setVerticalScrollBarEnabled(false);
        ScrollView.LayoutParams params = new ScrollView.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        container.setOrientation(LinearLayout.VERTICAL);
        scrollView.addView(container, params);
        FrameLayout.LayoutParams params1 = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, CoocaaApplication.Div(600), Gravity.CENTER);
        addView(scrollView, params1);
        focusView.getBackground().setAlpha(0);
    }

    public void setTitleSelectedListener(OnTitleSelectedListener listener) {
        this.titleSelectedListener = listener;
    }

    public void setTitles(List<RP_WallpaperCategory.WallPaper_Category_Item> categorys) {
        if (categorys.size() < 7) {
            FrameLayout.LayoutParams params1 = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
            scrollView.setLayoutParams(params1);
        }
        container.removeAllViews();
        this.category_items = categorys;
        totalNum = categorys.size();
        for (RP_WallpaperCategory.WallPaper_Category_Item title : category_items) {
            if (title.categoryId.equals("mycollection"))
                addTitle(title.title, R.drawable.launcher_wallpaper_collect);
            else if (title.categoryId.equals("ownwallpaper"))
                addTitle(title.title, R.drawable.launcher_icon_usb);
            else
                addTitle(title);
        }
    }

    private void addTitle(String title, int id) {
        TextView view = new TextView(context);
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.setTextSize(CoocaaApplication.Dpi(35));
        view.setTextColor(Color.WHITE);
        view.setText(title);
        view.setGravity(Gravity.CENTER_VERTICAL);
        view.setOnKeyListener(this);
        view.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0);
        view.setCompoundDrawablePadding(CoocaaApplication.Div(30));
        view.setPadding(CoocaaApplication.Div(20),
                CoocaaApplication.Div(20), CoocaaApplication.Div(20), CoocaaApplication.Div(20));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                CoocaaApplication.Div(300), ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
        container.addView(view, params);
        focusChangedEvent.registerView(view, revision, this);
    }

    private void addTitle(RP_WallpaperCategory.WallPaper_Category_Item item) {
        LinearLayout layout = new LinearLayout(context);
        layout.setFocusable(true);
        layout.setFocusableInTouchMode(true);
        layout.setGravity(Gravity.CENTER_VERTICAL);
        layout.setOnKeyListener(this);
        layout.setPadding(CoocaaApplication.Div(20),
                CoocaaApplication.Div(20), CoocaaApplication.Div(20), CoocaaApplication.Div(20));
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(CoocaaApplication.Div(300),
                ViewGroup.LayoutParams.WRAP_CONTENT);
        container.addView(layout, layoutParams);

        CoocaaDraweeView draweeView = new CoocaaDraweeView(context);
        if (item.title.contains("全部"))
            draweeView.getHierarchy().setPlaceholderImage(R.drawable.wallpaper_icon_all);
        LinearLayout.LayoutParams draweeParams = new LinearLayout.LayoutParams(CoocaaApplication.Div(40), CoocaaApplication.Div(40));
        layout.addView(draweeView, draweeParams);
        draweeView.setImageURI(Uri.parse(item.iconUrl));

        TextView view = new TextView(context);
        view.setTextSize(CoocaaApplication.Dpi(35));
        view.setTextColor(Color.WHITE);
        view.setText(item.title);
        view.setGravity(Gravity.CENTER_VERTICAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
        params.leftMargin = CoocaaApplication.Div(30);
        layout.addView(view, params);
        focusChangedEvent.registerView(layout, revision, this);

    }

    private void addPointView() {
        if (pointView == null) {
            pointView = new ImageView(context);
            pointView.setBackgroundResource(R.drawable.launcher_wallpaper_title_point);
            LayoutParams params = new LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL);
            params.leftMargin = CoocaaApplication.Div(65);
            pointView.setFocusableInTouchMode(true);
            pointView.setFocusable(true);
            addView(pointView, params);
            pointView.setVisibility(View.GONE);
            pointView.setOnFocusChangeListener(this);
        }
    }


    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (category_items != null) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_DOWN:
                        if (currentNum != totalNum - 1) {
                            currentNum++;
                            if (titleSelectedListener != null)
                                titleSelectedListener.titleSelected(currentNum, category_items.get(currentNum).title);
                            container.getChildAt(currentNum).requestFocus();
                        }
                        return true;
                    case KeyEvent.KEYCODE_DPAD_UP:
                        if (currentNum != 0) {
                            currentNum--;
                            if (titleSelectedListener != null)
                                titleSelectedListener.titleSelected(currentNum, category_items.get(currentNum).title);
                            container.getChildAt(currentNum).requestFocus();
                        }
                        return true;
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                        if (titleSelectedListener != null)
                            titleSelectedListener.onRightKey();
                        break;
                }
            }
        }
        return false;
    }

    public void setFocus(int pos) {
        currentNum = pos;
        postDelayed(new Runnable() {
            @Override
            public void run() {
                focusView.getBackground().setAlpha(255);
                container.getChildAt(currentNum).requestFocus();
            }
        }, 300);
    }

    public void showTitleBar() {
        if (pointView.getVisibility() == View.VISIBLE)
            pointView.requestFocus();
    }

    public void resetFocus() {
        int[] pos = new int[2];
        View view = container.getChildAt(currentNum);
        if (view != null) {
            view.requestFocus();
            view.getLocationOnScreen(pos);
            focusView.getBackground().setAlpha(0);
            directlySetFocus(view, pos[0], pos[1]);
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    focusView.getBackground().setAlpha(255);
                }
            }, 200);
        }
    }
    public void enablePointViewFocus(boolean focus){
        pointView.setFocusable(focus);
        pointView.setFocusableInTouchMode(focus);
    }

}
